package com.cskaoyan.market.admin.controller;

import com.cskaoyan.market.admin.service.MarketAdminAdminService;
import com.cskaoyan.market.admin.service.MarketAdminRoleService;
import com.cskaoyan.market.admin.service.impl.MarketAdminAdminServiceImpl;
import com.cskaoyan.market.admin.service.impl.MarketAdminRoleServiceImpl;
import com.cskaoyan.market.admin.vo.AdminRoleVo;
import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketRole;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/role/*")
public class AdminRoleController extends HttpServlet {
    private MarketAdminRoleService marketAdminRoleService = new MarketAdminRoleServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/role/", "");
        switch (op) {
            case "options":
                options(req, resp);
                break;
            default:
                resp.setStatus(404);
        }
    }

    private void options(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<AdminRoleVo> marketRoleList = marketAdminRoleService.getOptions();
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.okList(marketRoleList)));
    }
}
